package com.ifp.opengate.boot.utils;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.cloud.gateway.filter.FilterDefinition;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.web.util.UriComponentsBuilder;

import com.ifp.opengate.boot.bean.*;

/**
 * @Classname RouteUtils
 * @Description TODO
 * @Date 2019/4/23 10:58
 * @Created by zzh
 *          <p>
 *          zhuzh@belink.com
 *          </p>
 * @Version 1.0
 */
public class RouteUtils {

    public static RouteDefinition assembleRouteDefinition(OpenGateRoute gateRoute) {
        try {
            // Trace.log(Trace.MODULE_COMMON, Trace.LEVEL_INFO, "route 转换开始..."+ gateRoute.getRouteId());
            RouteDefinition definition = new RouteDefinition();
            definition.setId(gateRoute.getRouteId());
            definition.setOrder(gateRoute.getRouteOrder());

            // 设置断言
            List<PredicateDefinition> pdList = new ArrayList<>();

            // 请求路径断言
            PredicateDefinition predicate = new PredicateDefinition();
            Map<String, String> map = new HashMap<>();
            // if (StringUtils.hasText(gateRoute.getRoutePath())) {
            predicate.setName("Path");
            map.put("pattern", gateRoute.getRoutePath());
            predicate.setArgs(map);
            pdList.add(predicate);
            // }

            List<RouteAssertListBean> routeAssertList = gateRoute.getRouteAssertList();
            routeAssertList.forEach(routeAssertListBean -> {
                PredicateDefinition tempDefinition = new PredicateDefinition();
                tempDefinition.setName(routeAssertListBean.getAssertName());

                Map<String, String> argsMap = new HashMap<>();
                List<AssertListBean> assertList = routeAssertListBean.getAssertList();
                assertList.forEach(
                    assertListBean -> argsMap.put(assertListBean.getAssertKey(), assertListBean.getAssertValue()));
                tempDefinition.setArgs(argsMap);
                pdList.add(tempDefinition);
            });
            definition.setPredicates(pdList);

            // 设置过滤器
            List<FilterDefinition> filters = new ArrayList<>();

            List<RouteFilterListBean> routeFilterList = gateRoute.getRouteFilterList();
            routeFilterList.forEach(routeFilter -> {
                FilterDefinition filterDefinition = new FilterDefinition();
                filterDefinition.setName(routeFilter.getFilterName());

                Map<String, String> argsMap = new HashMap<>();
                List<FilterListBean> filterList = routeFilter.getFilterList();
                filterList.forEach(
                    assertListBean -> argsMap.put(assertListBean.getFilterKey(), assertListBean.getFilterValue()));
                filterDefinition.setArgs(argsMap);
                filters.add(filterDefinition);
            });
            definition.setFilters(filters);

            URI uri = null;
            if (gateRoute.getRouteUrl().startsWith("http")) {
                uri = UriComponentsBuilder.fromUri(URI.create(gateRoute.getRouteUrl())).build().toUri();
            } else {
                uri = URI.create(gateRoute.getRouteUrl());
            }
            definition.setUri(uri);
            return definition;
        } catch (Exception e) {
            // Trace.log(Trace.MODULE_TRACE, Trace.LEVEL_INFO, "deploy route error:【{}】", e);
        }
        return null;
    }

}
